package com.gotop.api.controller;

import com.gotop.api.service.ConfigService;
import com.gotop.common.base.BaseController;
import com.gotop.common.exception.BaseException;
import com.gotop.common.utils.R;
import com.gotop.common.utils.auth.CemUser;
import com.gotop.vo.config.ConfigParamVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @program: cem
 * @description: 接口应用配置
 * @author: superfish
 * @create: 2021-01-04 14:42
 **/
@RestController
@AllArgsConstructor
@RequestMapping("config")
@Api(value = "应用配置接口", tags = "配置接口")
public class ConfigController extends BaseController {

    @Autowired
    private ConfigService configService;

    @PostMapping("whether")
    @ApiOperation(value = "是否已创建应用")
    public R whether(@RequestBody ConfigParamVO configParamVO) {
        if (configService.whether(configParamVO)) {
            return R.data(0, "应用已存在");
        }
        return R.data(1, "可以新建应用");
    }

    @PostMapping("create")
    @ApiOperation(value = "创建应用")
    public R create(@RequestBody ConfigParamVO configParamVO) {
        final CemUser user = user();
        try {
            if (configService.create(user, configParamVO)) {
                return R.success("创建成功");
            }
            return R.fail("创建失败");
        } catch (BaseException e) {
            return R.data(e);
        }
    }

}
